<!--
 * @Descripttion: 房源分类
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 13:50:19
-->
<template>
    <div class="lb-app-house-classify">
        <top-nav></top-nav>
        <div class="page-main">
            <el-row class="page-top-operate">
                <lb-button type='primary' @click="addEditHouseClassify('add')" icon='el-icon-plus'>新增分类</lb-button>
            </el-row>
             <el-table
                v-loading='loading'
                :data="tableData"
                :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
                style="width: 100%">
                <el-table-column
                    prop="id"
                    label="ID">
                </el-table-column>
                <el-table-column
                    prop="title"
                    label="分类名">
                </el-table-column>
                <el-table-column
                    prop="type"
                    label="详情页样式">
                    <template slot-scope="scope">
                      <div>{{scope.row.type === 0 ? '二手房样式' : scope.row.type === 1 ? '简洁样式' : '新房样式'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="address"
                    label="图标">
                    <template slot-scope="scope">
                        <lb-image  :src="scope.row.img" />
                    </template>
                </el-table-column>
                <el-table-column
                    prop="top"
                    label="排序值">
                </el-table-column>
                <el-table-column
                    prop="status"
                    label="是否启用">
                    <template slot-scope="scope">
                        <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="updateClassify(scope.row.status,scope.row.id)"
                        ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                        <div class="table-operate">
                            <lb-button type='primary' size='mini' plain @click="addEditHouseClassify('edit', scope.row.id)">{{$t('action.edit')}}</lb-button>
                            <lb-button type='danger' size='mini' plain @click="confirmDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                            <lb-button type='success' size='mini' plain @click="$router.push(`${appPath}/house/editHouse?cate=${scope.row.id}&type=${scope.row.type}`)">添加房源</lb-button>
                        </div>
                  </template>
                </el-table-column>
            </el-table>
            <lb-page
            :batch='false'
            :page='page'
            :pageSize='limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
            </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      appPath: '',
      loading: false,
      tableData: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  created () {
    let {path} = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let {page, limit} = this
      this.$api.getHouseClassifyList({page, limit}).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.total = res.data.total
          this.tableData = res.data.data
        }
      })
    },
    addEditHouseClassify (type, id) {
      let {appPath} = this
      let url = type === 'add' ? `${appPath}/house/editClassify` : `${appPath}/house/editClassify?id=${id}`
      this.$router.push(url)
    },
    updateClassify (status, id) {
      this.$api.getEditHouseClassify({status, id}).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(this.$t('tips.successOper'))
        } else {
          this.getTableDataList()
        }
      })
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.delHouseClassify({id}).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
            this.getTableDataList()
          }
        })
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
    .lb-app-house-classify{
        width: 100%;
        .page-main{
            width: 100%;

        }
    }
</style>
